﻿using System.Collections.Generic;
using System.Linq;
using DAL.Common;
using DAL.POCO;
using Dapper;
using DapperExtensions;

namespace DAL.Repositories
{
    public class AnswerRepository
    {
        public static IEnumerable<Answer> GetAnswers(int questionId)
        {
            string sql = "SELECT id, questionId, mark, text, correct FROM [Answer] WHERE questionId = @id";

            var answers = MyConnection.Current.Query<Answer>(sql, new { id = questionId });
            return answers;
        }

        public static Answer GetAnswer(int answerId)
        {
            string sql = "SELECT id, questionId, mark, text, correct FROM [Answer] WHERE id = @id";

            var answer = MyConnection.Current.Query<Answer>(sql, new { id = answerId });
            return answer.Single();
        }

        public static void Insert(Answer answer)
        {
            MyConnection.Current.Insert<Answer>(answer);
        }

        public static void Update(Answer answer)
        {
            MyConnection.Current.Update<Answer>(answer);
        }

        public static void Delete(int answerId)
        {
            string sql = "DELETE FROM [Answer] WHERE id = @id";

            MyConnection.Current.Execute(sql, new { id = answerId });
        }
    }
}
